﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MacomberMapSystem.Common.Processing;
using System.Drawing;

namespace MacomberMapSystem
{
    /// <summary>
    /// (C) 2012, Electric Reliability Council of Texas, Inc.
    /// This class provides a tool strip status label to highlight the process state
    /// </summary>
    public class ProcessLabel : ToolStripStatusLabel
    {
        #region Variable declarations

        /// <summary>The process associated with our label</summary>
        public CIM_Processor Process;

        /// <summary>Our last state</summary>
        public CIM_Processor.enumProcessState LastState = CIM_Processor.enumProcessState.NotRun;
        #endregion

        /// <summary>
        /// Initialize a new process label
        /// </summary>
        /// <param name="Process"></param>
        /// <param name="ssMain"></param>
        public ProcessLabel(CIM_Processor Process, StatusStrip ssMain)
        {
            this.Process = Process;
            this.Text = this.Process.ProcessName;
            LastState = Process.State;
            ssMain.Items.Add(this);
        }

        /// <summary>
        /// Paint our label with the appropriate text
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            if (LastState != Process.State)
            {
                LastState = Process.State;
                this.Text = Process.ProcessName + ": " + Process.State.ToString();
            }

            //Determine our background color
            Color BackColor = Color.Yellow;
            Color ForeColor = Color.Black;
            if (Process.State == CIM_Processor.enumProcessState.Running || Process.State == CIM_Processor.enumProcessState.SendingEmail || Process.State == CIM_Processor.enumProcessState.UploadingToSharepoint)
            {
                BackColor = Color.LightGreen;
                ForeColor = Color.Black;
                Process.UpdateProcess();
            }
            else if (Process.State == CIM_Processor.enumProcessState.Completed)
                BackColor = Parent.BackColor;
            else if (Process.State == CIM_Processor.enumProcessState.CompletedWithErrors || Process.State == CIM_Processor.enumProcessState.FailedWithErrors)
                BackColor = Color.Red;
            
            RectangleF DrawRect = new RectangleF(0f, 0f, Process.Progress * this.Width, this.Height);
            if (Process.State == CIM_Processor.enumProcessState.Completed || Process.State == CIM_Processor.enumProcessState.CompletedWithErrors || Process.State == CIM_Processor.enumProcessState.FailedWithErrors)
                DrawRect.Width = this.Width;
            if (!float.IsNaN(DrawRect.Width) && DrawRect.Width > 0)
                if (Process.State == CIM_Processor.enumProcessState.Running)
                    using (Brush sB = new System.Drawing.Drawing2D.LinearGradientBrush(DrawRect, BackColor, this.BackColor, 0f))
                        e.Graphics.FillRectangle(sB, DrawRect);
                else                                       
                    using (Brush sB = new SolidBrush(BackColor))
                        e.Graphics.FillRectangle(sB, DrawRect);
                    
            ControlPaint.DrawBorder3D(e.Graphics, 0, 0, this.Width, this.Height, Border3DStyle.Flat);
            using (Brush ForeBrush = new SolidBrush(ForeColor))
                e.Graphics.DrawString(this.Text, this.Font, ForeBrush, new RectangleF(0, 0, this.Width, this.Height), MacomberMapSystem.Common.User_Interfaces.One_Lines.MM_OneLine_Element.CenterFormat);
      
            
                
        }

    }
}
